//
//  GoodsInOrderDetailViewController.m
//  TryShopping
//
//  Created by panquanReabam.com on 16/9/21.
//  Copyright © 2016年 RuiBen. All rights reserved.
//

#import "GoodsInOrderDetailViewController.h"
#import "ZJSwitch.h"
#import "UIViewExt.h"
#import "HttpRequest.h"
#import "UIImageView+WebCache.h"
#import "RDVTabBarController.h"
#import "MJRefresh.h"
#import "AllotOrderDetailModel.h"
#import "GoodsInOrderDetailView.h"
#import "AllotOrderDisposeViewController.h"
#import "GoodsInOrderWhsInViewController.h"
@interface GoodsInOrderDetailViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UITextViewDelegate,UISearchBarDelegate,UIScrollViewDelegate,AllotOrderDisposeViewControllerDelegate,GoodsInOrderDetailViewControllerDelegate,GoodsInOrderWhsInViewControllerDelegate>
@end

@implementation GoodsInOrderDetailViewController
{
    GoodsInOrderDetailView*DetailView;
    UIScrollView*MYmainScroll;
    UIButton*operationsBtn;
    NSArray*operationsArray;
    NSString*optType;
    NSArray*ColorArray;
    NSDictionary*dataDict;
    UIView*operationsView;
    bool saveAccount;
}
-(void)BackAction:(UIButton *)sender{
    
    NSUserDefaults*pushJudge = [NSUserDefaults standardUserDefaults];
    if([[pushJudge objectForKey:@"push"]isEqualToString:@"push"]) {
        NSUserDefaults * pushJudge = [NSUserDefaults standardUserDefaults];
        [pushJudge setObject:@""forKey:@"push"];
        [pushJudge synchronize];//记得立即同步
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=NO;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    self.title = @"进货详情";
    self.view.backgroundColor=[UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     backColor,
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
}

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    [self update:@""];
}
- (void)update:(NSString *)str{
    [operationsBtn setTitle:@"" forState:UIControlStateNormal];
    [operationsBtn removeFromSuperview];
    [MYmainScroll removeFromSuperview];
    MYmainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, ScreenWidth, ScreenHeight)];
    [MYmainScroll setBackgroundColor: zuiqianhuiColor];
    MYmainScroll.delegate = self;
    MYmainScroll.userInteractionEnabled = YES;
    MYmainScroll.scrollEnabled = YES;
    MYmainScroll.showsVerticalScrollIndicator = FALSE;
    MYmainScroll.showsHorizontalScrollIndicator = FALSE;
    [self.view addSubview:MYmainScroll];
    
    
    [super viewDidLoad];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    
    [param setValue:self.orderId forKey:@"orderId"];
    
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"GoodsIn/Order/Detail" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                
                NSLog(@"----%@",severDataDict.dataDict);
                [Global hideProgressHUDInternal:self.view];
                dataDict=severDataDict.dataDict;
                [_mDelegate update:@""];
                
                DetailView=[[GoodsInOrderDetailView alloc]init];
        
                AllotOrderDetailModel*dataModel=[AllotOrderDetailModel dataModelWithAllotOrderDetailDict:severDataDict.dataDict];
                DetailView.model=dataModel;
                DetailView.frame=CGRectMake(0, 0, ScreenWidth, DetailView.heightView.bottom);
                [MYmainScroll addSubview:DetailView];
                MYmainScroll.contentSize = CGSizeMake(ScreenWidth, DetailView.bottom+100);
                
               if ([[[dataDict objectForKey:@"orderData"]objectForKey:@"status"]isEqualToString:@"YB"]){
                    [self operations];
                }else if ([[[dataDict objectForKey:@"orderData"]objectForKey:@"status"]isEqualToString:@"YO"]){
                    [self operations];
                }
                
                
                
            }else{
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
    
}
-(void)operations:(UIButton*)sender{
    
    saveAccount=!saveAccount;
    if (saveAccount) {

    
   if ([[[dataDict objectForKey:@"orderData"]objectForKey:@"status"]isEqualToString:@"YB"]){
        operationsArray=@[@"进货入库"];
        [self Btn];
        
    }else if ([[[dataDict objectForKey:@"orderData"]objectForKey:@"status"]isEqualToString:@"YO"]){
        operationsArray=@[@"进货入库"];
        [self Btn];
        
    }
    
    }else{
    
        [operationsView removeFromSuperview];

    }
    
}

-(void)Btn{
    
    operationsView=[[UIView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, 110)];
    operationsView.backgroundColor=[UIColor clearColor];
    [self.view addSubview:operationsView];
    UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBoad)];
    singleFingerOne.numberOfTouchesRequired = 1; //手指数
    singleFingerOne.numberOfTapsRequired = 1; //tap次数
    singleFingerOne.delegate = self;
    [self.view addGestureRecognizer:singleFingerOne];
    ColorArray =[NSArray arrayWithObjects:[UIColor colorWithRed:78/255.0 green:112/255.0 blue:22/255.0 alpha:1.0],[UIColor colorWithRed:90/255.0 green:130/255.0 blue:26/255.0 alpha:1.0],nil];
    for (int i = 0; i<operationsArray.count; i++) {
        
        UIButton *optName = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-168/2*(i+1),0, 168/2, 110)];
        [optName setTitle:operationsArray[i]  forState:UIControlStateNormal];
        optName.tag=i;
        [optName setTitleColor:[UIColor whiteColor] forState:0];
        optName.titleLabel.font=[UIFont systemFontOfSize:13];
        optName.backgroundColor=ColorArray[i];
        [optName addTarget:self action:@selector(btnTouchUpInsid:) forControlEvents:UIControlEventTouchUpInside];
        [operationsView addSubview:optName];
    }
    
    
}


-(void)btnTouchUpInsid:(UIButton*)sender{
    [operationsView removeFromSuperview];
    
    if ([operationsArray[sender.tag] isEqualToString:@"进货入库"]) {
                        GoodsInOrderWhsInViewController *detailViewController = [[GoodsInOrderWhsInViewController alloc] init];
                        detailViewController.orderId=self.orderId;
                        detailViewController.mDelegate=self;
                        detailViewController.titleName=@"确认入库";
                        detailViewController.Dic=dataDict;
                        [self.navigationController pushViewController:detailViewController animated:YES];
                        
                        
        }
    
    
}

-(void)closeKeyBoad
{
    [operationsView removeFromSuperview];
}
-(void)operations{
    
    operationsBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0,  60/2, 60/2)];
        [operationsBtn setTitle:@"···" forState:UIControlStateNormal];
    operationsBtn.titleLabel.font=[UIFont systemFontOfSize:28];
    [operationsBtn setTitleColor:shenhuiColor forState:UIControlStateNormal];
    [operationsBtn addTarget:self action:@selector(operations:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:operationsBtn];
    self.navigationItem.rightBarButtonItem = myBtnBtn;
    
    
}

@end
